<template>
  <el-dialog
    :title="!dataForm.blId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="患者编号" prop="hzId">
        <el-input v-model="dataForm.hzId" placeholder="患者编号"></el-input>

      </el-form-item>
      <el-form-item label="创建人编号" prop="ysIdFirst">
        <el-input v-model="dataForm.ysIdFirst" placeholder="创建人编号"></el-input>

      </el-form-item>
      <el-form-item label="当前处理人编号" prop="ysIdCurrent">
        <el-input v-model="dataForm.ysIdCurrent" placeholder="当前处理人编号"></el-input>

      </el-form-item>

      <el-form-item label="创建人姓名" prop="ysNameFirst">
        <el-input v-model="dataForm.ysNameFirst" placeholder="创建人姓名"></el-input>

      </el-form-item>
      <el-form-item label="当前处理人姓名" prop="ysNameCurrent">
        <el-input v-model="dataForm.ysNameCurrent" placeholder="当前处理人姓名"></el-input>

      </el-form-item>
      <el-form-item label="状态" prop="blState">
        <el-input v-model="dataForm.blState" placeholder="状态"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      time: '',
      visible: false,
      dataForm: {
        blId: 0,
        hzId: '',
        ysIdFirst: '',
        ysIdCurrent: '',
        blCreateDate: '',
        ysNameFirst: '',
        ysNameCurrent: '',
        blState: '',
        blUpdateTime: ''
      },
      dataRule: {
        hzId: [
          {required: true, message: '患者编号不能为空', trigger: 'blur'}
        ],
        ysIdFirst: [
          {required: true, message: '创建人编号不能为空', trigger: 'blur'}
        ],
        ysIdCurrent: [
          {required: true, message: '当前处理人编号不能为空', trigger: 'blur'}
        ],
        ysNameFirst: [
          {required: true, message: '创建人姓名不能为空', trigger: 'blur'}
        ],
        ysNameCurrent: [
          {required: true, message: '当前处理人姓名不能为空', trigger: 'blur'}
        ],
        blState: [
          {required: true, message: '状态不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.blId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.blId) {
          this.$http({
            url: this.$http.adornUrl(`/hospital/bl/info/${this.dataForm.blId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.hzId = data.bl.hzId
              this.dataForm.ysIdFirst = data.bl.ysIdFirst
              this.dataForm.ysIdCurrent = data.bl.ysIdCurrent
              this.dataForm.blCreateDate = data.bl.blCreateDate
              this.dataForm.ysNameFirst = data.bl.ysNameFirst
              this.dataForm.ysNameCurrent = data.bl.ysNameCurrent
              this.dataForm.blState = data.bl.blState
              this.dataForm.blUpdateTime = data.bl.blUpdateTime
            }
          })
        }
      })
    },
    getNowTime () {
      const yy = new Date().getFullYear()
      const MM = (new Date().getMonth() + 1) < 10 ? '0' + (new Date().getMonth() + 1) : (new Date().getMonth() + 1)
      const dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
      const HH = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()
      const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
      const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
      this.time = yy + '-' + MM + '-' + dd + ' ' + HH + ':' + mm + ':' + ss
      return this.time
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/hospital/bl/${!this.dataForm.blId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'blId': this.dataForm.blId || undefined,
              'hzId': this.dataForm.hzId,
              'ysIdFirst': this.dataForm.ysIdFirst,
              'ysIdCurrent': this.dataForm.ysIdCurrent,
              'blCreateDate': this.getNowTime(),
              'ysNameFirst': this.dataForm.ysNameFirst,
              'ysNameCurrent': this.dataForm.ysNameCurrent,
              'blState': this.dataForm.blState,
              'blUpdateTime': this.getNowTime()
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
